package me.yxy.mobanddd.domain.transportorder;

import me.yxy.mobanddd.controller.request.query.TransportPageRequest;
import me.yxy.mobanddd.domain.shipment.Shipment;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

public interface TransportOrderRepository extends PagingAndSortingRepository<TransportOrder,Long>, QuerydslPredicateExecutor<TransportOrder> {

    Optional<TransportOrder> findByCode(String code);

    @Query(" FROM TransportOrder to WHERE to.shipment.id = ?1")
    List<TransportOrder> findByShipmentId(Long shipmentId, Pageable pageable);


    /**
     * 运单单表信息分页查询
     * @return
     */
    @Query(" FROM TransportOrder to ")
    Stream<TransportOrder> simplePageByCustomParam(@Param("param") TransportPageRequest param);

    @Query(" select count(1) from TransportOrder to WHERE to.shipment.id is not null AND to.id IN ?1")
    Integer countIfhasShipment(List<Long> orderIds);

    @Query(" select count(1) from TransportOrder to WHERE to.status = ?1 AND to.id IN ?2")
    Integer countIfFinished(TransportOrderStatus status,List<Long> orderIds);

    @Query(" select count(1) from TransportOrder to WHERE to.status = ?1 AND to.shipment = ?2")
    Integer countByStatusAndShipment(TransportOrderStatus status, Shipment shipment);

//    这里直接采用的Controller进来的TransportPageRequest，
//    虽然并非不可以，因为这和DDD的主体规范并不冲突，但严格来说，不太好，因为毕竟一个前端传参的规范，一个是后台关系型数据库的解析规范，中间还是会有语法差别
//    早期使用问题不大，但盲目套用，最终肯定是导致这个Request对象变得不伦不类，为了敢业务需求，加入一堆乱七八糟的适配参数
}
